import bankData from './data.js';
export default {
	/**
	 * 得到银行编码
	 * { String } cardNumber 银行卡号
	 */
	getBankCode: function (cardNumber) {
		let data = fetch(`https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=${cardNumber}&cardBinCheck=true`)
			.then(res => res.json())
			.then(res => res)
			.catch(err => console.log(err))
		return data;

	},

	/**
	 * 根据编码找到所属行
	 * { String } code 银行编码
	 */
	getImageColor(imgSrc) {
		const canvas = document.createElement('canvas');
		const ctx = canvas.getContext("2d");

		return new Promise((resolve, reject) => {
			const img = new Image();
			img.crossOrigin = 'anonymous';
			img.onload = () => {
				canvas.width = img.width;
				canvas.height = img.height;
				ctx.drawImage(img, 0, 0);

				const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
				const data = imageData.data;

				let r = 1, g = 1, b = 1;
				const pixelCount = data.length / 4;

				for (let i = 0; i < data.length; i += 4) {
					r += data[i];
					g += data[i + 1];
					b += data[i + 2];
				}

				r /= pixelCount;
				g /= pixelCount;
				b /= pixelCount;

				r = Math.round(r);
				g = Math.round(g);
				b = Math.round(b);

				const colorObj = { r, g, b };
				resolve(colorObj);
			};

			img.onerror = (error) => {
				reject(error);
			};

			img.src = "data:image/png;base64," + imgSrc;
		});
	},
	/**
	 * 求图片主题颜色
	 * @param {String} canvasID 画布ID
	 * @param {String} imgID 图片ID
	 * @param {String} imgSrc 图片资源路径
	 * */
	getImageColo: function (canvasID, imgID, imgSrc) {
		let imgWidth,
			imgHeight;
		let canvasWidth,
			canvasHeight;
		var canvas = uni.createCanvasContext(canvasID);
		// 获取img的宽度/高度
		var pro = function () {
			return new Promise((resolve, reject) => {
				uni.createSelectorQuery().select("#" + imgID).boundingClientRect(ret => {
					imgWidth = ret.width;
					imgHeight = ret.height;
					// 赋值canvas宽度/高度
					canvasWidth = imgWidth;
					canvasHeight = imgHeight;
					canvas.drawImage(imgSrc, 0, 0, canvasWidth, canvasHeight);
					canvas.draw(true);
					setTimeout(() => {
						// 获取像素数据
						uni.canvasGetImageData({
							// #ifdef MP-WEIXIN
							canvasId: canvas.canvasId,
							// #endif
							// #ifndef MP-WEIXIN
							canvasId: canvas.id,
							// #endif
							x: 0,
							y: 0,
							width: imgWidth,
							height: imgHeight,
							success: (res) => {
								let data = res.data;
								// console.log(data)
								var r = 1, g = 1, b = 1;
								// 取所有像素的平均值
								for (var row = 0; row < imgHeight; row++) {
									for (var col = 0; col < imgWidth; col++) {
										// console.log(data[((img.width * row) + col) * 4])
										if (row == 0) {
											r += data[((imgWidth * row) + col)];
											g += data[((imgWidth * row) + col) + 1];
											b += data[((imgWidth * row) + col) + 2];
										} else {
											r += data[((imgWidth * row) + col) * 4];
											g += data[((imgWidth * row) + col) * 4 + 1];
											b += data[((imgWidth * row) + col) * 4 + 2];
										}
									}
								}
								// 求取平均值
								r /= (imgWidth * imgHeight);
								g /= (imgWidth * imgHeight);
								b /= (imgWidth * imgHeight);
								// 将最终的值取整
								r = Math.round(r);
								g = Math.round(g);
								b = Math.round(b);
								let obj = { r, g, b }
								resolve(obj);
							},
							fail: (fail) => {
								reject(fail);
							}
						});
					}, 300);

				}).exec();
			}).catch(e => {
				console.log(e)
			})
		}
		return pro();
	},

	getBankNameByBankCode: function (code) {
		let bankName
		Object.keys(bankData).forEach(key => {
			if (key == code) {
				bankName = bankData[key]
			}
		})

		return bankName
	},

	/**
	 * 根据银行卡号得到所属行以及银行的logo(logo不是全不都有)
	 */
	getBankInfo: function (bankCode) {
		return new Promise((resolve, reject) => {
			this.getBankCode(bankCode).then(res => {
				if (res.validated) {
					// 银行编码
					const bank = res.bank
					const bankName = this.getBankNameByBankCode(bank)
					let bankType = ''
					switch (res.cardType) {
						case 'DC':
							bankType = '储蓄卡'
							break
						case 'CC':
							bankType = '信用卡'
							break
						case 'SCC':
							bankType = '准贷记卡'
							break
						case 'PC':
							bankType = '预付费卡'
							break
						default:
							bankType = ''
					}

					if (bankName == undefined) {
						// 没有找到对应的银行
						resolve({
							code: 1,
							data: null,
							message: '不支持此银行'
						})
					} else {
						resolve({
							code: 0,
							data: {
								bank,
								bankName,
								bankType
							},
							message: 'success'
						})
					}
				} else {
					// 银行卡没有查到相关信息
					resolve({
						code: 1,
						data: null,
						message: '没有查到银行卡相关信息，请检查！'
					})
				}
			}).catch(err => {
				console.log(err)
			})
		})
	},

	/**
	 * 根据银行名的到logo
	 */
	getBankLogoByBankName: function (bankName) {
		let str = ''
		Object.keys(bankData).forEach(key => {
			if (bankData[key] == bankName) {
				str = key
			}
		})
		return str
	},




}